package org.dromara.run.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.run.domain.ActivityForm;
import org.springframework.stereotype.Service;
import org.dromara.run.domain.bo.ApplicationBo;
import org.dromara.run.domain.vo.ApplicationVo;
import org.dromara.run.domain.Application;
import org.dromara.run.mapper.ApplicationMapper;
import org.dromara.run.service.IApplicationService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 免跑申请表Service业务层处理
 *
 * @author Lion Li
 * @date 2024-08-30
 */
@RequiredArgsConstructor
@Service
public class ApplicationServiceImpl implements IApplicationService {

    private final ApplicationMapper baseMapper;

    /**
     * 查询免跑申请表
     */
    @Override
    public ApplicationVo queryById(Long applicationId){
        return baseMapper.selectVoById(applicationId);
    }

    /**
     * 查询免跑申请表列表
     */
    @Override
    public TableDataInfo<ApplicationVo> queryPageList(ApplicationBo bo, PageQuery pageQuery) {
        QueryWrapper<Application> lqw = buildQueryWrapper(bo);
        Page<ApplicationVo> result = baseMapper.selectApplicationList(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询免跑申请表列表
     */
    @Override
    public List<ApplicationVo> queryList(ApplicationBo bo) {
        QueryWrapper<Application> lqw = buildQueryWrapper(bo);
        return baseMapper.selectApplicationList(lqw);
    }

    private QueryWrapper<Application> buildQueryWrapper(ApplicationBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<Application> lqw =new QueryWrapper();
        lqw.eq(StringUtils.isNotBlank(bo.getMemberId()), "pra.member_id", bo.getMemberId());
        lqw.eq(StringUtils.isNotBlank(bo.getScheduleId()), "pra.schedule_id ", bo.getScheduleId());
        lqw.eq(StringUtils.isNotBlank(bo.getApproveStatus()), "pra.approve_status", bo.getApproveStatus());
        lqw.eq(bo.getApplicationId()!=null, "pra.application_id", bo.getApplicationId());
        lqw.between(params.get("beginApproveDate") != null && params.get("endApproveDate") != null,
            "pra.approve_date" ,params.get("beginApproveDate"), params.get("endApproveDate"));
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            "pra.create_time" ,params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.eq(StringUtils.isNotBlank(bo.getYears()), "pbtc.years", bo.getYears());
        lqw.like(StringUtils.isNotBlank(bo.getStudentName()), "pmi.name", bo.getStudentName());
        lqw.like(StringUtils.isNotBlank(bo.getSemester()), "prs.semester", bo.getSemester());
        lqw.eq(StringUtils.isNotBlank(bo.getClassId()), "pmi.class_id", bo.getClassId());
        lqw.eq(StringUtils.isNotBlank(bo.getProfessionalId()), "sd1.dept_id", bo.getProfessionalId());
        lqw.eq(StringUtils.isNotBlank(bo.getCollegeId()), "sd2.dept_id", bo.getCollegeId());
        return lqw;
    }

    /**
     * 新增免跑申请表
     */
    @Override
    public Boolean insertByBo(ApplicationBo bo) {
        Application add = MapstructUtils.convert(bo, Application.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setApplicationId(add.getApplicationId());
        }
        return flag;
    }

    /**
     * 修改免跑申请表
     */
    @Override
    public Boolean updateByBo(ApplicationBo bo) {
        Application update = MapstructUtils.convert(bo, Application.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Application entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除免跑申请表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            for (Long id:ids) {
                ApplicationVo applicationVo = baseMapper.selectVoById(id);
                if(applicationVo!=null&&!"0".equals(applicationVo.getApprover())){
                    throw new ServiceException("该记录已经审批完成无法删除");
                }
            }
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
